from rest_framework import serializers
from .models import Favorite
from apps.chefs.serializers import ChefSerializer

class FavoriteSerializer(serializers.ModelSerializer):
    chef = ChefSerializer(read_only=True)
    
    class Meta:
        model = Favorite
        fields = ['id', 'chef', 'created_at']
        read_only_fields = ['id', 'created_at']

class FavoriteCreateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Favorite
        fields = ['chef']
        
    def validate_chef(self, value):
        if Favorite.objects.filter(
            user=self.context['request'].user,
            chef=value
        ).exists():
            raise serializers.ValidationError("已经收藏过该厨师")
        return value